package com.zhangshuo.patternDesign.obverser;

import java.util.Observable;
import java.util.Observer;

/**
 * Created by shuo.zhang on 2017-10-22.
 */
public class Reader implements Observer {

    private String name;

    public Reader(String name){
        super();
        this.name = name;
    }


    /**
     * 订阅
     * @param writer
     */
    public void subcribe(Writer writer){
        writer.addObserver(this);
    }

    public void subcribe(String writerName){
        Writer writer =WriterManager.getInstance().getWriter(writerName);
        subcribe(writer);
    }

    /**
     *取消订阅
     * @param writer
     */
    public void unSubcribe(Writer writer){
        writer.deleteObserver(this);
    }

    public void unSubcribe(String writerName){
        Writer writer = WriterManager.getInstance().getWriter(writerName);
        unSubcribe(writer);
    }




    @Override
    public void update(Observable o, Object arg) {
        if (o instanceof Writer){
            Writer writer = (Writer) o;
            System.out.println(this.name + " 知道了 " + writer.getName() + " 发布了新书 " + writer.getLastNovel() );
        }
    }


}
